package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcDictionaryEntity;
import com.dhcc.cdc.busi.service.ICdcDictionaryService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 业务字典
 * 
 * @ClassName: CdcDictionaryController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:07
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcdictionary")
public class CdcDictionaryController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcdictionary/";

	@Autowired
	private ICdcDictionaryService cdcDictionaryService;
	

    /**
     * 跳转到业务字典列表首页
     */
    @RequestMapping("")
    @Permission("cdcdictionary:list")
    public String index() {
        return PREFIX + "cdcdictionary.html";
    }
	
    /**
     * 跳转到新增业务字典
     */
    @RequestMapping("/to_add")
    @Permission("cdcdictionary:add")
    public String toAdd() {
        return PREFIX + "cdcdictionary_add.html";
    }
	
	/**
     * 跳转到修改业务字典
     */
    @RequestMapping("/to_update/{cdcDictionaryId}")
    @Permission("cdcdictionary:update")
    public String toUpdate(@PathVariable Long cdcDictionaryId, Model model) {
        CdcDictionaryEntity cdcDictionary = this.cdcDictionaryService.selectById(cdcDictionaryId);
        model.addAttribute("cdcDictionary",cdcDictionary);
        return PREFIX + "cdcdictionary_edit.html";
    }

    /**
     * 获取业务字典列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcdictionary:list")
    public Object list(@RequestParam Map<String, Object> params) {
        List<CdcDictionaryEntity> result = null;
        Page<CdcDictionaryEntity> page = new PageFactory<CdcDictionaryEntity>().defaultPage();
        if((CommonUtil.isNotEmpty(params.get("NotQuery")))&&(params.get("NotQuery").toString().equals("true"))){
            result = new ArrayList<CdcDictionaryEntity>();
        }else{
            result = this.cdcDictionaryService.page(page,params);
        }
        page.setRecords((List<CdcDictionaryEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    @RequestMapping(value = "/query/{dataCode}/{typeCode}")
    @ResponseBody
    @Permission("cdcdictionary:list")
    public Object queryByTypeCode(@PathVariable String dataCode,@PathVariable String typeCode) {
        List<CdcDictionaryEntity> result = this.cdcDictionaryService.queryByTypeCode(dataCode,typeCode);
        return result;
    }

    /**
     * 新增业务字典
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcdictionary:add")
    @BussinessLog(value = "新增业务字典")
    public Object add(CdcDictionaryEntity cdcDictionary) {
        if (CommonUtil.isOneEmpty(cdcDictionary)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcDictionary.insert();
        return R.ok();
    }

    /**
     * 删除业务字典
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcdictionary:delete")
    @BussinessLog(value = "删除业务字典")
    public Object delete(@RequestParam Long cdcDictionaryId) {
	
        this.cdcDictionaryService.deleteById(cdcDictionaryId);

        return R.ok();
    }

    /**
     * 修改业务字典
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcdictionary:update")
    @BussinessLog(value = "修改业务字典")
    public Object update(CdcDictionaryEntity cdcDictionary) {
        if (CommonUtil.isOneEmpty(cdcDictionary)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcDictionaryEntity old = this.cdcDictionaryService.selectById(cdcDictionary.getId());

        old.setCode(cdcDictionary.getCode());
        old.setDescription(cdcDictionary.getDescription());
        old.setDictypeId(cdcDictionary.getDictypeId());
        old.setIsActive(cdcDictionary.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
